import { _modify } from '../../common/api/todos'
const app = getApp()
Page({
  data: {
    states: ['跟进', '来访', '认筹', '认购', '签约'],
    current: null,
    lastState: '',
    id: null,
    disabled: true,
    remark: ''
  },
  change (e) {
    let current = e.detail.current
    this.data.current = current
  },
  onInput (e) {
    this.data.remark = e.detail.value
  },
  modify () {
    const { states, current, id, remark } = this.data
    const uid = this.data.userinfo.AgentInfo.UnionID
    if (!current) {
      app.toast('请先选择状态')
      return
    }
    if (!remark.trim()) {
      app.toast('请填写备注')
      return
    }
    wx.showModal({
      title: '温馨提示',
      content: '确定修改吗？',
      success: r => {
        if (r.confirm) {
          app.loading('加载中')
          const state = states[current]
          _modify(uid, id, state, remark)
            .then(res => {
              wx.hideLoading()
              wx.showModal({
                title: res.data.IsSuccess?'温馨提示':'对不起',
                content: res.data.Msg,
                showCancel: false,
                success: rr => {
                  if (res.data.IsSuccess && rr.confirm) {
                    wx.navigateBack()
                  }
                }
              })
            })
            .catch(err => {
              wx.hideLoading()
              wx.showModal({
                title: '对不起',
                content: JSON.stringify(err),
                showCancel: false
              })
            })
        }
      }
    })
  },
  onLoad (options) {
    this.data.id = options.id
    this.data.lastState = options.state
    this.setData({
      lastState: this.data.lastState
    })
  },
  onReady () {},
  onShow () {
    app.launch().then(res => {
      this.setData({
        userinfo: res,
        disabled: false
      })
    }).catch(err => {
      console.log(err)
      if (err === '用户未注册') {
        wx.showModal({
          title: '对不起',
          content: '您还未注册',
          showCancel: false,
          success: r => {
            if (r.confirm) {
              wx.redirectTo({
                url: '/pages/index/index'
              })
            }
          }
        })
      }
    })
  },
  onShareAppMessage () {}
})